<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="时间" prop="startTime">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd"
          type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
          clearable>
        </el-date-picker>
      </el-form-item>
      <el-form-item label="开票员" prop="userId">
        <el-select v-model="queryParams.userId" clearable placeholder="请选择人员">
          <el-option v-for="dict in users" :key="dict.userId" :label="dict.nickName" :value="dict.userId" />
        </el-select>
      </el-form-item>
      <el-form-item label="客户名称" prop="purchaserName">
        <el-input v-model="queryParams.purchaserName" placeholder="请输入客户名称,模糊匹配" clearable
          style="width: 200px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="登录账号" prop="username">
        <el-input v-model="queryParams.username" placeholder="请输入登录账号,模糊匹配" clearable
          style="width: 200px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客户编号" prop="businessCode">
        <el-input v-model="queryParams.businessCode" placeholder="请输入客户编号,精准匹配" clearable
          style="width: 200px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="省" prop="province">
        <el-select v-model="queryParams.province" clearable @change="provinceChange">
          <el-option v-for="dict in provinceList" :key="dict.value" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="市" prop="city">
        <el-select v-model="queryParams.city" clearable @change="cityChange">
          <el-option v-for="dict in cityList" :key="dict.value" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="区/县" prop="county">
        <el-select v-model="queryParams.county" clearable>
          <el-option v-for="dict in countyList" :key="dict.value" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini"
          @click="handleEditTable(undefined)" v-hasPermi="['system:role:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
          @click="handleEditTable" v-hasPermi="['system:role:edit']">修改</el-button>
      </el-col>
      -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini"
          @click="handlePurchaserLogininfoExport" v-hasPermi="['statistics:login:purchaser:export']">导出
        </el-button>
      </el-col>
    </el-row>

    <el-table border v-loading="loading" :data="purchaserLogins">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="客户编号" prop="businessCode" width="80" />
        <el-table-column label="客户名称" prop="name" :show-overflow-tooltip="true" width="220" />
        <el-table-column label="登录账号" prop="username" :show-overflow-tooltip="true" width="120" />
        <el-table-column label="客户类型" prop="moldName" :show-overflow-tooltip="true" width="120" />
        <el-table-column label="省" prop="provinceName" :show-overflow-tooltip="true" width="120" />
        <el-table-column label="市" prop="cityName" :show-overflow-tooltip="true" width="120" />
        <el-table-column label="区/县" prop="countyName" :show-overflow-tooltip="true" width="120" />
        <el-table-column label="详细地址" prop="address" :show-overflow-tooltip="true" width="230" />
        <el-table-column label="登录次数" prop="loginCount" align="center" width="80" />
        <el-table-column label="下订数量" prop="orderCount" align="center" width="80" />
        <!--
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)"
              v-hasPermi="['system:role:edit']">
              <span class="el-dropdown-link">
                <i class="el-icon-d-arrow-right el-icon--right"></i>更多
              </span>
              <el-dropdown-menu slot="dropdown">

              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
        -->
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize" @pagination="getList" />

  </div>
</template>

<script>
import { getRegionList } from "@/api/region"
import { pagePurchaserLogininfo } from "@/api/statisticsAdmin/purchaserLogin"
import { listUser } from "@/api/system/user";

export default {
  name: 'PurchaserLoginList',
  data () {
    return {
      dateRange: ['', ''],
      queryParams: {
        province: '',
        city: '',
        county: '',
        userId: '',
        total: 1,
        pageNum: 1,
        pageSize: 10,
      },
      regionList: [],
      provinceList: [],
      cityList: [],
      countyList: [],
      hasOrderList: [{ value: '是' }, { value: '否' }],
      loading: false,
      total: 0,
      purchaserLogins: [],
      users: [],
    }
  },
  created () {
    this.handleRegionList()

    this.dateRange = this.$route.params.dateRange.split('~')
    this.getList()
    this.getUsers()
  },
  methods: {
    getUsers(){
      listUser({postIds:1, pageSize: 100}).then(res=>{
        this.users = res.rows
      })
    },
    handleRegionList(){
      getRegionList().then(res=>{
        res.data.forEach(region=>{
          if('province' === region.level){
            this.provinceList.push({id: region.id, name: region.name, orderNum: region.orderNum})
          } else{
            this.regionList.push({id: region.id, name: region.name, parentId: region.parentId, level: region.level, orderNum: region.orderNum})
          }
        })
        this.provinceList.sort(region.orderNum)
        this.regionList.sort(region.orderNum)
      })
    },
    provinceChange(){
      this.cityList = []
      this.regionList.forEach(region=>{
        if('city' === region.level){
          if(region.parentId == this.queryParams.province){
            this.cityList.push(region)
          }
        }
      })
    },
    cityChange(){
      this.countyList = []
      this.regionList.forEach(region=>{
        if('district' === region.level){
          if(region.parentId == this.queryParams.city){
            this.countyList.push(region)
          }
        }
      })
    },
    getList () {
      if(this.dateRange){
        this.queryParams.startDate = this.dateRange[0]
        this.queryParams.endDate = this.dateRange[1]
      }
      this.loading = true
      pagePurchaserLogininfo(this.queryParams).then(response=>{
        this.purchaserLogins = response.rows;

        this.total = response.total;
        this.loading = false;
      })
    },
    resetQuery () {
      this.queryParams.purchaserName = ''
      this.queryParams.username = ''
      this.queryParams.businessCode = ''
      this.queryParams.province = ''
      this.queryParams.city = ''
      this.queryParams.county = ''
      this.queryParams.userId = ''

      this.getList()
    },
    handlePurchaserLogininfoExport(){
      if(this.dateRange){
        this.queryParams.startDate = this.dateRange[0]
        this.queryParams.endDate = this.dateRange[1]
      }

      this.download('/statistics/logininfo/export', {
        ...this.queryParams
      }, `客户登录数据_${this.parseTime(new Date().getTime(), "{y}-{m}-{d}_{h}{i}{s}")}.xlsx`)
    },
  },
}
</script>

<style lang="scss" scoped>
</style>


